package edu.mercer.sse661.ao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;

import com.google.common.base.Function;
import com.google.common.base.Throwables;

/**
 * Download URL contents to a StringBuilder.<p>
 *
 * @see Function
 */

public class DownloadURLFunction implements Function<String,StringBuilder>
{
	public StringBuilder apply( final String urlStr )
	{
		return downloadURL( urlStr );
		
	}//method

	
	/** 
	 * Download URL contents to StringBuilder.<p>
	 * 
	 * @param urlStr - String URL; Example: 'http://www.somewebsite.net'
	 */
	
	private StringBuilder downloadURL( final String urlStr )
	{
		StringBuilder contentDownloaded = new StringBuilder();
		
		try
		{
			final URL url = new URL( urlStr );
			final URLConnection conn = url.openConnection();

			conn.setRequestProperty("Host", url.getHost() );
	
			conn.setRequestProperty("User-Agent", 
					"Mozilla/5.0 (Macintosh; U; PPC Mac "
							+ "OS X 10_6_1; appLanguage) "
							+ "AppleWebKit/531.21.10 (KHTML, like"
							+ " Gecko) Version/4.0.4 Safari/"
							+ "531.21.10");

			conn.setRequestProperty("Content-Type", 
					"application/x-www-form-urlencoded; "
							+ "charset=UTF-8");
			
			conn.setRequestProperty("Accept-Charset", 
					"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			
			conn.setRequestProperty("Accept", 
					"text/html,application/xhtml+xml,"
							+ "application/xml;q=0.9,*/*;q=0.8");
			
			conn.setRequestProperty("Keep-Alive", "10");
			
			conn.setRequestProperty("Connection", "keep-alive");

			final BufferedReader buffRdr = new BufferedReader(
					new InputStreamReader( conn.getInputStream(), "UTF-8"));

			String lineIn = "";

			Integer downloaded = 0;
			
			while( ( lineIn = buffRdr.readLine() ) != null )
			{
				contentDownloaded.append( lineIn );
				
				downloaded += lineIn.length();

			}//while
			
			LoggerCustom.obtainLog( "download_url_function", 
									Level.ALL)
						.fine( "downloaded site: " 
								+ url.getHost() 
								+ " content size: " 
								+ downloaded );

		}
		catch( final IOException err )
		{
			throw new RuntimeException( "error accessing website: " + urlStr 
					+ " : " + Throwables.getStackTraceAsString( err ) );
			
		}//try
		
		return contentDownloaded;
		
	}//method	

	
}//class
